package com.ebeitech.auction.application;

import android.app.Activity;
import android.app.Application;
import android.content.Context;
import android.content.SharedPreferences;

import com.baidu.mapapi.SDKInitializer;
import com.ebeitech.auction.db.CommonDBHelper;
import com.ebeitech.auction.domain.dao.UserDao;
import com.ebeitech.library.util.StringUtil;
import com.ebeitech.auction.domain.User;
import com.ebeitech.library.constant.SPConstants;
import com.ebeitech.library.util.SPManager;
import com.tencent.imsdk.TIMLogLevel;
import com.tencent.qcloud.presentation.business.InitBusiness;

import java.util.ArrayList;
import java.util.List;
//import com.ebeitech.com.ebeitech.library.util.StringUtil;
//import com.ebeitech.com.ebeitech.library.util.ThreadFactory;
//import com.netease.nis.bugrpt.CrashHandler;
//import com.netease.nis.bugrpt.user.UserStrategy;
//import com.xiaomi.channel.commonutils.logger.LoggerInterface;
//import com.xiaomi.mipush.sdk.Logger;

/**
 * Created by alice on 2017/10/24.
 */

public class EbeiApplication extends Application {

    private static Context context;
    private static List<Activity> mActivitys = new ArrayList<Activity>();
    /**
     * 登陆用户
     */
    private static User mUser;
    /**
     * 登陆用户名 ，数据库名
     */
    private static String mUserAccount;

    /**
     * Application 实例
     */
    private static EbeiApplication cipApplication;

    @Override
    public void onCreate() {
        super.onCreate();
        this.context = this;
        cipApplication = this;
        mUser = new User();
        mUserAccount = SPManager.getInstance(this).get(SPConstants.USER_ACCOUNT,"");
        initData();

        //本地异常捕捉
        CrashHandler crashHandler = CrashHandler.getInstance();
        crashHandler.init(getApplicationContext());

        // 初始化地图
        SDKInitializer.initialize(this);

        // 初始化腾讯云
        SharedPreferences pref = getSharedPreferences("data", MODE_PRIVATE);
        int loglvl = pref.getInt("loglvl", TIMLogLevel.DEBUG.ordinal());
        //初始化IMSDK
        InitBusiness.start(getApplicationContext(),loglvl);
        //初始化TLS
//        TlsBusiness.init(getApplicationContext());

//        ThreadFactory.builder();

        //网易云捕
//        UserStrategy strategy = new UserStrategy(getApplicationContext());
//        strategy.setUserId(getUser().getUserName());
//        strategy.setUserTag("ebei");
//        strategy.setVersionSuffix("ebei");
//        CrashHandler.init(getApplicationContext(),strategy);

//        LoggerInterface newLogger =new LoggerInterface() {
//            @Override
//            public void setTag(String s) {
//
//            }
//
//            @Override
//            public void log(String s) {
//                Log.d("xiaomi", s);
//            }
//
//            @Override
//            public void log(String s, Throwable t) {
//
//                Log.d("xiaomi", s, t);
//            }
//        };
    }

    @Override
    public void onTerminate() {
        super.onTerminate();
    }

    public EbeiApplication getCipApplication() {
        return cipApplication;
    }

    public static Context getContext() {
        return context;
    }

    /**
     * 获取登陆用户
     * @return
     */
    public static User getUser() {
        if(mUser == null || StringUtil.isEmpty(mUser.getUserAccount())){
            User user = UserDao.getInstance().getUserByAccount(SPManager.getInstance().get(SPConstants.USER_ACCOUNT,""));
            if(user != null && !StringUtil.isEmpty(user.getUserAccount())){
                setUser(user);
            }
        }
        return mUser;
    }

    /**
     * 设置登陆用户
     * 设置数据库
     * @param user
     */
    public static void setUser(User user) {
        mUser = user;
        mUserAccount = user.getUserAccount();

        SPManager.getInstance().put(SPConstants.USER_ACCOUNT,user.getUserAccount());
        SPManager.getInstance().put(SPConstants.USER_ID,user.getUserId());
        SPManager.getInstance().put(SPConstants.USER_NAME,user.getUserName());
        SPManager.getInstance().put(SPConstants.USER_PASSWORD,user.getPassword());
        SPManager.getInstance().put(SPConstants.USER_SIG,user.getUserSig());

        SPManager.getInstance().put(SPConstants.CLIENT_TOKEN, user.getToken());

        CommonDBHelper.changeDBHelper(mUserAccount);
    }

    public static String getUserAccount() {
        return mUserAccount;
    }

	/**
     * 初始化一些变量
     */
    public static void initData() {
        SPManager.getInstance().put("isDialogShowing", false);
        SPManager.getInstance().put("dialogCount", 0);
    }

    public static void addActivity(Activity activity) {
        mActivitys.add(activity);
    }

    public static void removeActivity(Activity activity) {
        mActivitys.remove(activity);
    }

    public static boolean isAppAlive() {
        return mActivitys.size() > 0;
    }

}